class Solution{//34 O(LogN) O(1)
public:
vector<int> searchRange(vector<int>& a, int t){
int L = lower_bound(a.begin(), a.end(), t) - a.begin();
if (L == (int)a.size() || a[L] != t) return { -1, -1 };
int R = upper_bound(a.begin(), a.end(), t) - a.begin() - 1;
return { L, R };
}
};
Binary Search 學習中 加油吧 又收到一個機會,但先好好準備再go,決戰十月底maybe
每次比對後把搜尋區間對半縮小
中點(l+r)/2
≥t答案可能在更左,繼續往左找
l<=r;當 r=2 時 l>r,表搜尋區間已空(沒有更左的位置可找),l=3 就是最左邊符合的索引。